//-----------------------------------------------------------------------------------
#include "game.h"
//-----------------------------------------------------------------------------------
int TestGame::m_iPos;
Map TestGame::m_kTileMap;
Player TestGame::m_kPlayer;
StateMachine * TestGame::m_kFSMPlayer;
int TestGame::m_iMineGold = MINE_GOLD_MAX;
int TestGame::m_iTownGold = 0;
//-----------------------------------------------------------------------------------
TestGame::TestGame()
:
m_ikStartRow(0),
m_ikStartCol(0),
m_fpkStartPosX(0),
m_fpkStartPosY(0)
{

}
//-----------------------------------------------------------------------------------
TestGame::~TestGame() 
{ 

}
//-----------------------------------------------------------------------------------
bool TestGame::OnInit() 
{
	_clearScreen = true;

	m_kFSMPlayer = new StateMachine(STATES_COUNT, EVENTS_COUNT, STATE_NONE);

	m_kFSMPlayer->SetRelation(STATE_NONE,			EVENT_TOMINE,		STATE_WALK_TO_MINE);
	m_kFSMPlayer->SetRelation(STATE_WALK_TO_MINE,	EVENT_STARTMINING,	STATE_MINING);
	m_kFSMPlayer->SetRelation(STATE_MINING,			EVENT_TOTOWN,		STATE_WALK_TO_TOWN);
	m_kFSMPlayer->SetRelation(STATE_WALK_TO_TOWN,	EVENT_UNLOAD,		STATE_INTOWN);
	m_kFSMPlayer->SetRelation(STATE_INTOWN,			EVENT_TOMINE,		STATE_WALK_TO_MINE);
	m_kFSMPlayer->SetRelation(STATE_INTOWN,			EVENT_TOIDLE,		STATE_NONE);

	m_kFSMPlayer->SetUpdateFunc(STATE_NONE, NULL);
	m_kFSMPlayer->SetUpdateFunc(STATE_WALK_TO_MINE, Player::WalkToMine);
	m_kFSMPlayer->SetUpdateFunc(STATE_MINING, Player::Mining);
	m_kFSMPlayer->SetUpdateFunc(STATE_WALK_TO_TOWN, Player::ReturnFromMine);
	m_kFSMPlayer->SetUpdateFunc(STATE_INTOWN, Player::Dump);

// 	m_kFSMPlayer->SetDrawFunc(STATE_NONE, Player::DrawMiner);
// 	m_kFSMPlayer->SetDrawFunc(STATE_WALK_TO_MINE, Player::DrawMiner);
// 	m_kFSMPlayer->SetDrawFunc(STATE_MINING, Player::DrawMiner);
// 	m_kFSMPlayer->SetDrawFunc(STATE_INTOWN, Player::DrawMiner);

	if(!m_kTileMap.Load("data/graphics/classic-2.bmp", "data/graphics/AStarMapa.map", 0))
	{
		return false;
	}

	m_kTileMap.SetPos(-230, 300);

	int mapWidth = m_kTileMap.GetMapWidth();
	int mapHeight = m_kTileMap.GetMapHeight();

	m_iPos = 0;

	m_fpAcum = 0.0f;
	m_fpTime = 0.0002f;
	
 	if(!m_kPlayer.Start(mapWidth, mapHeight))
 	{
 		return false;
 	}

	return true;
}
//-----------------------------------------------------------------------------------
void TestGame::OnFrame()
{
	float fTimeBetweenFrames = (float)_fpsMeter.GetDT();

	_fpsMeter.SetLockFPS(60);

	if(m_fpAcum > 0.5f)
	{
		m_kFSMPlayer->Update();

		m_fpAcum = 0.0f;
	}

	m_fpAcum += m_fpTime;

	m_kTileMap.Update(fTimeBetweenFrames);

	if(KeyDown(DIK_ESCAPE))
	{
		PostQuitMessage(0);
	}
}
//-----------------------------------------------------------------------------------
void TestGame::OnDraw() 
{
	wstringstream ss;

	ss << ZAK_ENGINE_NAME << endl << "FPS: " << _fpsMeter.GetFps() << endl;
	ss << endl << endl << "Gold in Mine: " << m_iMineGold << " / " << MINE_GOLD_MAX;
	ss << endl << "Gold in Player: " << Player::m_iGold;
	ss << endl << "Gold in Town: " << m_iTownGold;

	m_kTileMap.Draw();

	g_renderer.SetFontColor(0xFFFF0000);
	g_renderer.DrawString(ss.str(), 10, 10, 800-20, 600-20, ZAK_TEXT_LEFT);
}
//-----------------------------------------------------------------------------------
bool TestGame::OnShutdown() 
{
	return true;
}
//-----------------------------------------------------------------------------------